package com.summer.framework.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.framework.bill.entity.BillType;
import com.summer.framework.bill.mapper.BillTypeMapper;
import com.summer.framework.bill.service.BillTypeService;
import com.summer.framework.common.enums.BaseStateEnmu;
import com.summer.framework.common.enums.bill.BillStatus;
import com.summer.framework.common.exception.NotFoundException;
import com.summer.framework.common.service.BaseServiceImpl;
import com.summer.framework.common.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * @auther: 杨杰
 * @date: 2019/2/21 14:19
 * @description:
 */
@Service
@Slf4j
public class BillTypeServiceImpl extends BaseServiceImpl<BillTypeMapper, BillType> implements BillTypeService {

    @Override
    public List<BillType> list(Wrapper<BillType> queryWrapper) {
        return super.baseMapper.selectList(new QueryWrapper<BillType>().eq("state", BaseStateEnmu.SUBMIT.getId()));
    }

    @Override
    public boolean save(BillType entity) {
        entity.setId(UUIDUtil.create());
        entity.setState(BaseStateEnmu.NORMAL.getId());
        return super.save(entity);
    }

    @Override
    public boolean submit(String id) throws NotFoundException {
        if (StringUtils.isEmpty(getById(id))) {
            throw new NotFoundException(BillStatus.BILL_ID_NOT_FOUND.getCode(), BillStatus.BILL_ID_NOT_FOUND.getMessage());
        } else {
            BillType billType = new BillType(id);
            billType.setState(BaseStateEnmu.SUBMIT.getId());
            updateById(billType);
            return true;
        }
    }
}
